package mapEditor.interactor;

import debug.*;
	
import mapEditor.MapEditor;
import mapEditor.map.MapCanvas;
import mapEditor.obj.DrawObject;
import mapEditor.utils.MapLocation;

import java.awt.Graphics2D;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.InputEvent;

import org.apache.batik.swing.gvt.InteractorAdapter;

// ********************************************************************
// *** CLASS: MapInteractorAdapter
// ********************************************************************
public abstract class MapInteractorAdapter extends InteractorAdapter //implements MouseMotionListener
{ public static final int MEASURE=0;   // Mouse-mode: Measuring Distance by pointing (mouse down) and dragging a rectangle.
  public static final int EDIT=1;      // Mouse-mode: Editing.
  public static final int ZOOM=2;      // Mouse-mode: Editing.
  public static final int PAN=3;      // Mouse-mode: Editing.
  public static final int DRAW_LINE=4; // Mouse-mode: Draw a Line by pointing (mouse down) and dragging.
  public static final int DRAW_POLYGON=5; // Mouse-mode: Draw a Polygon by repetive pointing (mouse click).
  public static final int DRAW_ELLIPSE=6; // Mouse-mode: Draw a ...
  public static final int DRAW_PARALLELOGRAM=7; // Mouse-mode: Draw a ...
  public static final int DRAW_PATH=8; // Mouse-mode: Draw a ...
  public static final int DRAW_TEXT=9; // Mouse-mode: Draw a ...
  public abstract String getMouseModeMnemonic();
  
  protected MapCanvas mapCanvas;

  private int mouseMode; // The current mouse functional mode.
  protected MapLocation mouseStart,mouseCurrent;


  // ******************************************************************
  // *** CONSTRUCTOR: MapInteractorAdapter
  // ******************************************************************
  public MapInteractorAdapter(MapCanvas mapCanvas,int mouseMode)
  { this.mapCanvas=mapCanvas;
    this.mouseMode=mouseMode;
    this.mouseCurrent=MapLocation.newPixelPoint(mapCanvas.getMapGrid(),0,0);
  }
  
  private boolean active=true;
  public void shutDown() { active=false; }
  public boolean startInteraction(InputEvent ie)
  {
	  return(active);
  }
  public boolean endInteraction() { return(!active); }
  
  // ******************************************************************
  // *** METHOD: getMouseMode
  // ******************************************************************
  public int getMouseMode()
  { return(mouseMode); }

  // ******************************************************************
  // *** METHOD: paintMouseEffect
  // ******************************************************************
  public abstract void paintMouseEffect(Graphics2D g);

  // ******************************************************************
  // *** METHOD: mouseDragged
  // ******************************************************************
  // Invoked when a mouse button is pressed and then dragged.
  public void mouseDragged(MouseEvent e)
  { mouseCurrent.setX(e.getX());
    mouseCurrent.setY(e.getY());
	MapEditor.getStatusLine().setMousePosition(mouseCurrent);
    mouseDragged();
  }
  protected abstract void mouseDragged();


  // ******************************************************************
  // *** METHOD: mouseMoved
  // ******************************************************************
  // Invoked when the mouse has been moved (with no buttons down).
  private Cursor pointCursor=Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  private Cursor crossHairCursor=Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
  private Cursor handCursor=Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
  public void mouseMoved(MouseEvent e)
  { mouseCurrent.setX(e.getX());
    mouseCurrent.setY(e.getY());
	MapEditor.getStatusLine().setMousePosition(mouseCurrent);
	Cursor oldCursor=mapCanvas.getCursor();
//	Cursor newCursor=(mapCanvas.withinMark()!=null)?crossHairCursor:pointCursor;
	Cursor newCursor=pointCursor;
	String newDescription="";
	if(mapCanvas.getMark(mouseCurrent)!=null) newCursor=crossHairCursor;
	else
	{ DrawObject obj=mapCanvas.getObject(mouseCurrent);
	  if(obj!=null) { newCursor=handCursor; newDescription=obj.getIdent(); }
	}
	if(newCursor!=oldCursor)
    { mapCanvas.setCursor(newCursor);
      mapCanvas.repaint();
    }
    MapEditor.getStatusLine().setDescription(newDescription);
    mouseMoved();
  }
  protected abstract void mouseMoved();

  // ******************************************************************
  // *** METHOD: mousePressed
  // ******************************************************************
  // Invoked when a mouse button has been pressed.
  public void mousePressed(MouseEvent e)
  { MapLocation p=MapLocation.newPixelPoint(mapCanvas.getMapGrid(),e.getX(),e.getY());
    Debug.TRACE("MapInteractorAdapter.mousePressed: at "+p);
	int flg=e.getModifiers();
    if((flg& InputEvent.BUTTON1_MASK)!=0)
         mouseLeftPressed(p);
    else mouseRightPressed(p);
  }
  protected abstract void mouseLeftPressed(MapLocation p);
  protected abstract void mouseRightPressed(MapLocation p);

  // ******************************************************************
  // *** METHOD: mouseReleased
  // ******************************************************************
  // Invoked when a mouse button has been released.
  public void mouseReleased(MouseEvent e)
  { MapLocation p=MapLocation.newPixelPoint(mapCanvas.getMapGrid(),e.getX(),e.getY());
    int flg=e.getModifiers();
    if((flg& InputEvent.BUTTON1_MASK)!=0) mouseLeftReleased(p);
    else mouseRightReleased(p);
  }
  protected abstract void mouseLeftReleased(MapLocation p);
  protected abstract void mouseRightReleased(MapLocation p);
  
}